<?php
class MUser extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }


	function verifyUser($u,$pw){
		$this->db->select('uid,username,role_id');
		$this->db->where('username',db_clean($u,50));
		$this->db->where('password', db_clean(do_hash($pw),16));
		$this->db->where('activate', '1');
		$this->db->limit(1);
		$Q = $this->db->get('user');

		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			$_SESSION['uid'] = $row['uid'];
			$_SESSION['role_id'] = $row['role_id'];
			$_SESSION['username'] = $row['username'];
		}else{
			 $_SESSION['uid'] = 0; 
			 $_SESSION['role_id'] = 0;
			$this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');
		}		
	}
	function getRoles() {
		 $data = array();
	     $Q = $this->db->get('role');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['role_id']] = $row['title'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 		
	}
	function getUser($id) {
		$data = array();
	    $this->db->where('uid',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('user');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
	
	function getAdminUser($id){
     	$data = array();
	    $this->db->select('user.*, admin.firstname,admin.lastname,admin.gender');
	    $this->db->from('user');
	    $this->db->join('admin', 'user.uid = admin.uid');
	    $this->db->where('user.uid',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    

	}
	
	function getLecturerUser($id){
     	$data = array();
	    $this->db->select('user.*, lecturer.firstname,lecturer.lastname,lecturer.gender');
	    $this->db->from('user');
	    $this->db->join('lecturer', 'user.uid = lecturer.uid');
	    $this->db->where('user.uid',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    

	}
	
	function getAdminDepartmentUser($id){
     	$data = array();
	    $this->db->select('user.*, admin_department.firstname,admin_department.lastname,admin_department.gender');
	    $this->db->from('user');
	    $this->db->join('admin_department', 'user.uid = admin_department.uid');
	    $this->db->where('user.uid',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    

	}
	
	function getAllUsers(){
     $data = array();
     $this->db->select('user.*, role.title');
     $this->db->from('user');
     $this->db->join('role', 'user.role_id = role.role_id');
     $this->db->order_by("user.role_id", "asc");
     $Q = $this->db->get();
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
     }
     $Q->free_result();    
     return $data; 	
	}
	
	
	function addUser(){
      $data = array('username' => db_clean($_POST['username']),
                    'email' => db_clean($_POST['email']),
      				'password' => db_clean(do_hash($_POST['password']),16),
      				'role_id' => db_clean($_POST['role_id']),
                    'activate' => db_clean($_POST['activate'])
                    );
	  $this->db->set('date_created', 'NOW()', FALSE);
	  $this->db->insert('user',$data);
	  $uid = $this->db->insert_id();
	  if ($_POST['role_id']==1) {
	  	 $this->addAdminUser($uid);	  
	  }
	  elseif ($_POST['role_id']==2){
	  	 $this->addAdminDepartmentUser($uid); 
	  }
	  elseif ($_POST['role_id']==3){
	  	 $this->addLecturerUser($uid);
	  }
	}
	
	
	function addAdminUser($uid){
      $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender']),
      				'uid' => db_clean($uid)
                    );
	  $this->db->insert('admin',$data);	
	}
	
	function addAdminDepartmentUser($uid){
     $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender']),
      				'uid' => db_clean($uid)
                    );
                    
	  $this->db->insert('admin_department',$data);	
	}
	
	function addLecturerUser($uid){
      $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender']),
      				'uid' => db_clean($uid)
                    );
                    
	  $this->db->insert('lecturer',$data);	
	}
	
	function updateUser($role_id){
	  if($_POST['changepassword']==1) {
	      $data = array('username' => db_clean($_POST['username']),
	                    'email' => db_clean($_POST['email']),
	      				'password' => db_clean(do_hash($_POST['password']),16),
	      				'role_id' => db_clean($role_id)
	                    );
	  }else {
	  	   $data = array('username' => db_clean($_POST['username']),
	                    'email' => db_clean($_POST['email']),
	      				'role_id' => db_clean($role_id)
	                    );
	  }
	  
	  $this->db->where('uid',id_clean($_POST['uid']));
	   $this->db->set('date_modified', 'NOW()', FALSE);
	  $this->db->update('user',$data);	
	}
	
	function updateAdminUser(){
      $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender'])
                    );
	  $this->db->where('uid',id_clean($_POST['uid']));
	  $this->db->update('admin',$data);	
	}
	
	function updateAdminDepartmentUser(){
      $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender'])
                    );
	  $this->db->where('uid',id_clean($_POST['uid']));
	  $this->db->update('admin_department',$data);	
	}
	
	function updateLecturerUser(){
      $data = array('firstname' => db_clean($_POST['firstname']),
                    'lastname' => db_clean($_POST['lastname']),
      				'gender' => db_clean($_POST['gender'])
                    );
	  $this->db->where('uid',id_clean($_POST['uid']));
	  $this->db->update('lecturer',$data);	
	}
	
	
	function activateUser($id){
 	 $data = array('activate' => db_clean($_POST['activate']));
 	 $this->db->where('uid', id_clean($id));
	 $this->db->update('user', $data);
	
	}
	
	function inActiveUser($id){
 	 $data = array('activate' => db_clean('0'));
 	 $this->db->where('uid', id_clean($id));
	 $this->db->update('user', $data);
	
	}
}


?>